package com.example.PriceCompare;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseAdapter {

	//static final String KEY_NAME = "name";
	static final String KEY_ROWID = "_id";
	private static final String KEY_PRICE ="price"; 
	private static final String KEY_LAT ="lat";
	private static final String KEY_LOG ="log";
	private static final String KEY_ADDRESS = "address";
	private static final String KEY_DESC = "descript";
	private static final String TAG  = "DatabaseAdapter";
	
	private static final String DATABASE_CREATE =
		"create table items(_id integer primary key autoincrement,"+
		"address text not null,descript text not null,price integer,lat text not null,log integer);";
	private static final String DB_NAME = "item_list";
	private static final String DB_TABLE = "items";
	private static final int DB_VERSION = 2;
	
	private SQLiteDatabase mDb;
	private final Context mCtx;
	private DatabaseHelper mDbHelper;
	
	private static class DatabaseHelper extends SQLiteOpenHelper{

		DatabaseHelper(Context context){
			super(context, DB_NAME, null, DB_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE);
			// TODO Auto-generated method stub
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.v(TAG,"database version is being upgraded,all data will be lost ");
			db.execSQL("DROP TABLE IF EXISTS items");
			// TODO Auto-generated method stub
		}
		
	}
	
	public DatabaseAdapter(Context ctx){
		this.mCtx = ctx; 
	}
	public DatabaseAdapter open() throws SQLException{

		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}
	
	public void close(){
	
		mDbHelper.close();
	}
	
	public long createItem(String address,String descript,int price,String lat,int log){
		ContentValues initialize = new ContentValues();
		//initialize.put(KEY_NAME, name);
		initialize.put(KEY_ADDRESS, address);
		initialize.put(KEY_DESC, descript);
		initialize.put(KEY_PRICE, price);
		initialize.put(KEY_LAT, lat);
		initialize.put(KEY_LOG, log);
		return mDb.insert(DB_TABLE, null, initialize);
		
	}
	
	public boolean deleteItem(long rowId){
		return (mDb.delete(DB_TABLE, KEY_ROWID +"=" +rowId, null)==1);
	}
	
	public boolean updateItem(long rowId,String address,String descript,int price,String lat,int log){
		ContentValues args = new ContentValues();
		//args.put(KEY_NAME, name);
		args.put(KEY_ADDRESS, address);
		args.put(KEY_DESC, descript);
		args.put(KEY_PRICE, price);
		args.put(KEY_LAT, lat);
		args.put(KEY_LOG, log);
		return (mDb.update(DB_TABLE, args, KEY_ROWID + "=" + rowId, null))==1;
	}
	
	public Cursor getAllItems(){
		Cursor mCursor = mDb.query(true, DB_TABLE, new String[]{KEY_ROWID,KEY_ADDRESS,KEY_DESC,KEY_PRICE,KEY_LAT,KEY_LOG}, null, null, null, null, null,null);
		if(mCursor !=null)
			mCursor.moveToFirst();
		return mCursor;
		}
	
/*	public Cursor getItem(String search){
		Cursor mCursor = mDb.query(true, DB_TABLE, new String[]{KEY_ROWID,KEY_ADDRESS,KEY_DESC,KEY_PRICE,KEY_LAT,KEY_LOG}, KEY_NAME + "=" + search, null, null, null, null,null);
		if(mCursor != null)
			mCursor.moveToFirst();
		return mCursor;
	}*/
	
	public Cursor sortItem(){
		Cursor mCursor = mDb.query(DB_TABLE, new String[]{KEY_ROWID,KEY_ADDRESS,KEY_DESC,KEY_PRICE,KEY_LAT,KEY_LOG}, null, null, null, null,KEY_ADDRESS);
		if(mCursor != null)
			mCursor.moveToFirst();
		return mCursor;
	}
}

	
